<template>
    <div class="parentsForm">
      <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
      <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
      <el-dialog title="新增家长" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="parents" label-width="80px" :rules="addParentsRules" ref="addParentsForm">
        <el-form-item label="头像">
          <el-upload
            class="upload"
            :action="getUploadUrl"
            list-type="picture-card"
            :limit="1"
            accept="image/*"
            :on-change="(file,fileList)=>{imgHandleChange(file,fileList,1,this,'addParentsUploadForm','parents')}"
            :on-remove="(file,fileList)=>{imgHandleRemove(1,this)}"
            :on-before="(file,fileList)=>{imghandleBeforeUpload(file,1,this)}"
            :auto-upload="false"
            :file-list="fileList[0]"
            ref="file1">
            <i class="el-icon-plus"></i>
          </el-upload>
          <!--<viewer-dialog ref="viewer" :images="imagesUp"></viewer-dialog>-->
        </el-form-item>
        <el-form-item label="家长姓名" prop="pname">
          <el-col :span="16">
           <el-input v-model="parents.pname" placeholder="请填入姓名" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="家长性别" prop="psex">
          <el-col :span="16">
            <el-input v-model="parents.psex" placeholder="请填入性别" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="家长年龄" prop="page">
          <el-col :span="16">
            <el-input v-model="parents.page" placeholder="请填入年龄" clearable/>
          </el-col>
        </el-form-item>

        <el-form-item label="联系方式" prop="pphone">
          <el-col :span="16">
            <el-input v-model="parents.pphone" placeholder="联系方式" clearable/>
          </el-col>
        </el-form-item>

        <el-form-item label="密码" prop="ppwd">
          <el-col :span="16">
            <el-input v-model="parents.ppwd" placeholder="请填入密码" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="地址" prop="paddress">
          <el-col :span="16">
            <el-input v-model="parents.paddress" placeholder="请填入地址" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="成长值" prop="pgrowthvalue" style="display: none">
          <el-col :span="16">
            <el-input v-model="parents.pgrowthvalue" placeholder="请填入成长值" clearable/>
          </el-col>
        </el-form-item>
          <el-form-item label="个性邮箱" prop="pemail">
            <el-col :span="16">
              <el-input type="textarea" :rows="5" v-model="parents.pemail" placeholder="请输入邮箱" />
            </el-col>
          </el-form-item>
        <el-form-item label="个性签名" prop="pdescribe">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="parents.pdescribe" placeholder="请输入个性签名" />
          </el-col>
        </el-form-item>
      </el-form>
      <el-row>
         <el-col :span="12">
         <span>&nbsp;</span></el-col>
        <el-col :span="6">
           <span><el-button type="primary" @click="addParents('addParentsForm')">添加</el-button></span>
        </el-col>

    </el-row>
      </el-dialog>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
  import rules from "../../../../static/js/validator/rules.js"
  import utils from "../../../../static/js/utils/upload";
    var initfavOption = [];
    export default {
        name: "addParents",
        data(){
          return {parents:{"pgrowthvalue":0},
              checkAll: false,
              isIndeterminate: true,
              addParentsRules:{pname:[{required:true,message:"标题不能为空",trigger:'blur'}],
                psex:[{required:true,message:"标题不能为空",trigger:'blur'}],
                page:[{required:true,message:"标题不能为空",trigger:'blur'}],
                pphone:[{required:true,message:"标题不能为空",trigger:'blur'}],
                ppwd:[{required:true,message:"标题不能为空",trigger:'blur'}],
                pstate:[{required:true,message:"标题不能为空",trigger:'blur'}],
                paddress:[{required:true,message:"标题不能为空",trigger:'blur'}],
                pgrowthvalue:[{required:true,message:"标题不能为空",trigger:'blur'}],
                pemail:[{required:true,message:"标题不能为空",trigger:'blur'}]},
              addDialogIsShow:false,
            //上传使用
            fileList: [[],[],[]], //缓存区文件
            uploadFile:[[],[],[]], //  上传用文件
            formData:{files:[]},
            imagesUp:null,
            //上传使用结束

          }
          },
      methods: {
        init() {
          this.addDialogIsShow = true;
        },
        handleCheckAllChange(val) {
          this.parents.favs = val ? initfavOption : [];
          this.isIndeterminate = false;
        },
        handleCheckedFavsChange(value) {
          let checkedCount = value.length;
          this.checkAll = checkedCount === this.favs.length;
          this.isIndeterminate = checkedCount > 0 && checkedCount < this.favs.length;
        },
        //关闭页面
        handleClose() {
          this.addDialogIsShow = false;
          //给父组件传参
          this.$emit("addDialogIsShow", this.addDialogIsShow);
        },
        addParents:function(addParentsForm){
          this.$refs[addParentsForm].validate(valid => {//此处refs对象和标签上的ref对应
            if (valid) {
              //如果通过验证 to do...
              console.log(this.parents);
              alert(this.parents.nname+"添加成功");
              this.setDataFromAxios();
              this.handleClose();
            } else {
              console.log('error submit!!')
              return false
            }
          });
        },
        handleUpSuccess(response, file, fileList) {
          if (response.code == 200) {
            this.ruleForm.cardUpImg = response.data[0];
            this.imagesUp = response.data;
            this.hideUp = fileList.length == 1;
            this.$message.success(response.msg);
          }
        },
        uploadImg:function(modelName,name){
          //  使用formdata格式
          let formData = new FormData();
          formData.append("model",modelName);//图片上传子目录
          console.log(formData);
          console.log(this.formData.files[name]);
          if (this.formData.files[name]) {
            formData.append("file", this.formData.files[name]);
          }
          console.log("----------------------------");
          console.log(formData);
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios(this.$store.state.host+"/uploadImg",formData,"post",{headers: { "Content-Type": "multipart/form-data" }})
            .then(dto=>{this.$message.info("头像上传成功");
              if(dto.code==200){
                this.parents.pimg = dto.obj;
              }
            });//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        setDataFromAxios:function(){
         alert(this.$store.state.host);
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios("/api/parentsController/addParents",this.parents,"post")
            .then(res=>{this.leagues = res.leagues});//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        updateView(e) {
          this.$forceUpdate();
        },
        imghandleBeforeUpload(file,type,vm) {

          return utils.UPLOAD.Upload().handleBeforeUpload(file,type,vm);
        },
        imgHandleChange(file, fileList, type,vm,modelName,name) {
          if(vm.fileList[type].length!=0){
            alert("请先删除再提交")
          }
          return utils.UPLOAD.Upload().handleChange(file, fileList, type,vm,modelName,name);
        },
        imgHandleRemove(type,vm) {

          return utils.UPLOAD.Upload().handleRemove( type,vm);
        },
      },
      computed:{
        getUploadUrl(){
          return      this.$store.state.host+"/uploadImg";
        }
      },
      //监听
      watch: {
        addDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
          this.addDialogIsShow = newValue;
        }
      }
    }
</script>
<style scoped lang="stylus">
 .parentsForm
   margin 0 auto
   width 1000px
   text-align left
</style>
